#ifndef TCPCLIENT_H
#define TCPCLIENT_H

#include <QObject>
#include <QTcpSocket>
#include <QTimer>
#include <QFile>
#include <QFileInfo>

class TcpClient : public QObject
{
    Q_OBJECT
public:

    struct CmdHandler{
        const char* cmd;
        bool (TcpClient::*handle)(QByteArray &array);

    };
//    bool getState(){
//        return mIsConnected;
//    }
    bool isConnected(){
        return (mSocket->state()==QTcpSocket::ConnectedState);
    }
    void connectToHost();

    static TcpClient* getInstanse();
    ~TcpClient();

signals:
//    void getApic();
//    void picData(QByteArray& arr);
    void disconnected();
public slots:
    void onConnectTimeout();
    void writeTick();
    void onConnected();
    void onDisconnected();
    void disConnectFromHost();
    void read();
    //void readFile();
private:
    static TcpClient* mClient;
    bool handleReciveData(QByteArray &data);
    bool syncTime(QByteArray &array);
    bool updateRecord(QByteArray &array);
    const static CmdHandler cmdTable[];
    TcpClient(QObject *obj=0);

//    bool getPicInfo(QByteArray &data);
    void writeCmd(const QByteArray &cmd, const QString &name);
    QTcpSocket* mSocket;
    QTimer* mConnectTimer;
    QTimer* mTickTimer;
    int mTimeoutCnt;
//    bool mIsConnected;
//    QFile *mNewFile;
//    QString fileName;
//    qint64 fileSize;
//    qint64 picSize;
//    qint64 recevSize;//已经接收到文件的大小
//    QByteArray arr;
    
};

#endif // TCPCLIENT_H
